package com.jie.demo1.controller.userController;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jie.demo1.Sservice.UserService;
import com.jie.demo1.common.ulity.JWTUtil;
import com.jie.demo1.pojo.Result;
import com.jie.demo1.pojo.User;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "http://localhost:5173/")  //开启跨域请求
@RestController
@RequestMapping("/user")
public class userInfoController {




    @Autowired
    private UserService userService;
    //解析token查看当前用户信息，返回用户信息到前端抽屉中
    @PostMapping("/getUserInfo")
    public Result<User> getUserInfo(@RequestHeader("Authorization") String authorization)  {
        if (authorization == null || !authorization.startsWith("Bearer ")) {
            return Result.fail(301,"请求头报错");
        }
        //因为上面的token会有一个长度为7的前缀，这里直接从第八个开始获取，表示只获取token信息
        String token = authorization.substring(7);
        System.out.println("登陆后查看用户信息接口,token"+token);

        Claims claims = JWTUtil.checkToken(token);
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        User no = userService.getOne(userWrapper.eq(User::getNo, claims.get("no")));//getOne方法可以直接返回一个对象
        return Result.ok(no);
    }

}
